package com.mxl.service.cashorder.rule;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mxl.common.DESUtils;
import com.mxl.common.helper.ResponseErrorCode;
import com.mxl.common.rule.AbstractRule;
import com.mxl.service.cashorder.dto.CashOrderRequest;

public class CardNoAndPwdDecodeCheck extends AbstractRule {
	private static final Logger logger = LoggerFactory
			.getLogger(CardNoAndPwdDecodeCheck.class);

	@Override
	protected boolean makeDecision(Object obj) throws Exception {
		// 根据密钥解密
		CashOrderRequest c = (CashOrderRequest) obj;
		String key = c.getCustomMerchant().get(0).getMacKey();
		try {
			logger.debug("CardNoAndPwdDecodeCheck decode befor cardNo:"
					+ c.getCardNo() + "---cardPWD:" + c.getCardPwd()
					+ "-----key:" + key);
			c.setCardNo(DESUtils.decrypt(c.getCardNo(), key));
			logger.info("CardNoAndPwdDecodeCheck cardNO:" + c.getCardNo());
			c.setCardPwd(DESUtils.decrypt(c.getCardPwd(), key));
			logger.info("CardNoAndPwdDecodeCheck cardPWD:"
					+ (c.getCardPwd().length() > 6 ? c.getCardPwd().substring(
							0, 6) : c.getCardPwd()) + "*************");
		} catch (Exception e) {
			c.setMsgId(ResponseErrorCode.CARDNO_ANDPWD_DECODE_ERROR.getValue());
			logger.error("CardNoAndPwdDecodeCheck fail........", e);
			return false;
		}
		return true;
	}
}
